import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/network_image_load/load_image.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/components/my_gesture_detector.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_controller.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import '../../../components/yt_text_style.dart';

class SiteSettingView extends GetView<SiteSettingController> {
  SiteSettingView({Key? key}) : super(key: key);

  @override
  final controller = Get.put(SiteSettingController());

  Widget _listBody() {
    return ListView(
      physics: const AlwaysScrollableScrollPhysics(),
      children: [
        Container(
          margin: EdgeInsets.only(top: 20, left: 20.w, right: 20.w),
          // color: Colour.green,
          height: 498 - 82,
          child: _getSetItems(),
        )
      ],
    );
  }

  _getSetItems() {
    List<Widget> items = [];
    for (int i = 0; i < controller.settingItemsList.length; i++) {
      items.add(controller.settingItemsList[i]);
    }

    return GridView.builder(
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 16.0,
          mainAxisSpacing: 16.0,
          childAspectRatio: 1.4, // 设置宽高比
        ),
        itemCount: items.length,
        itemBuilder: (context, index) {
          return controller.settingItemsList[index];
        },
    );

  }


  Widget _generateHeader() {
    return Column(
      children: [
        SizedBox(
          height: 300.h,
          width: double.infinity,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                margin: EdgeInsets.only(
                    top: ScreenUtil().statusBarHeight, bottom: 12.h),
                height: 44.h,
                width: double.infinity,
                child: Stack(
                  children: [
                    IconButton(
                      icon: Icon(
                          Platform.isAndroid
                              ? Icons.arrow_back
                              : Icons.arrow_back_ios,
                          color: Colour.white),
                      onPressed: () => Get.back(),
                    ),
                    Align(
                      child: Text(
                        controller.siteName,
                        style: TextStyle(
                          color: Colour.white,
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    var value = ResKey.changePicture.tr;
    double width = TextWidth.calculateTextSize(
        value, ytTextStyle(color: Colors.white, fontSize: 12));
    return GetBuilder<SiteSettingController>(
      init: controller,
      builder: (_) {
        return Scaffold(
          backgroundColor: Colour.white,
          appBar: PreferredSize(
            preferredSize: Size(
              double.infinity,
              300.h,
            ),
            child: Container(
              height: 300.h,
              color: Colors.blue,
              child: Stack(
                alignment: Alignment.bottomCenter,
                children: <Widget>[
                  SizedBox(
                    height: 300.h,
                    child: LoadImage(
                      controller.imageString,
                      height: 300.h,
                      width: ScreenUtil().screenWidth,
                      fit: BoxFit.cover,
                      holderImg: 'assets/images/site_setting_top_banner',
                    ),
                  ),
                  _generateHeader(),

                  PermissionWidget(permissionStringList: const [PermissionConstant.homeSiteManageSettingChangeImg], child: Positioned(
                      bottom: 16,
                      right: 16,
                      child: MyGestureDetector(
                        onTap: () {
                          controller.askUserForImageSource(context);
                        },
                        child: Container(
                          decoration: BoxDecoration(
                              color: Colors.black.withOpacity(0.45),
                              borderRadius:
                              const BorderRadius.all(Radius.circular(14))),
                          alignment: Alignment.center,
                          height: 28,
                          width: width + 10,
                          child: Text(
                            value,
                            style:
                            ytTextStyle(color: Colors.white, fontSize: 12),
                          ),
                        ),
                      )))
                ],
              ),
            ),
          ),
          body: _listBody(),
        );
      },
    );
  }
}
